<?php
namespace Mentees\Controller;

class NeedController extends MainController {

    public function index(){
        $map['status']=1;
        $map['userID']=$this->userID;
        $obj = M('Needs');
        $count = $obj->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 10);
        $page->setConfig('prev',L('_prev_page'));
        $page->setConfig('next',L('_next_page'));
        $page->setConfig('first',L('_first_page'));
        $page->setConfig('end',L('_end_page'));
        $page->setConfig('theme','%first% %upPage% %linkPage% %downPage% %end%');
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();   
        $this->assign('list', $list);
        $this->assign('page', $show);
        
        $this->display();
    }

    //发布需求
    public function add(){
       if($_POST){
            $list = M('Mentees')->where('userid='.$this->userID)->find();
            $data['userID']=$this->userID;
            $data['userName'] = $list['nickname'];
            $data['userFace'] = $list['face'];
            $data['trainID']=I('post.trainID');
            $trainItem=M("TrainCate")->where("id=".$data['trainID']."")->getField("name");
            $data['trainItem']=$trainItem;
            $data['skills']=I('post.skills');
            $data['target']=I('post.target');
            $data['train']=I('post.train');
            $data['work']=I('post.work');
            $data['createTime'] = time(); 
            $list = M('Needs')->add($data);
            if ($list) {
                echo $this->echo_json_str("SUCCESS",L('_action_success'),U('Mentors/getTuijian',array('cate'=>$data['trainID'])));die;
            }else{
                echo $this->echo_json_str(L('_action_fail'));die;
            }
       }else{
            $map['show']=1;
            $train = M('TrainCate')->field('id,name')->where($map)->order('sort asc,id asc')->select();
            $this->assign('train',$train);
            $this->display(); 
       }
       
    }

    //编辑需求

    public function view(){

        $id=I("id");
        if(empty($id)){
            $this->error("Illegal access");
        }else{
          $map['id']=$id;
          $data['userID']=$this->userID;
          $list = M('Needs')->where($map)->find();
           if (!$list) {
                $this->error(L('_not_exist'));
           }else{
                $this->assign('list',$list); 
                $this->display(); 
           }
        }
    }
    //删除需求
    public function del(){
       $id=I('id');
        if($id==''){
             $this->error(L('_no_selected'));
        }else{
            $obj = M('Needs');
            $where['id'] = $id;
            $where['userID']=$this->userID;
            $obj->where($where)->delete();
            $this->success(L('_action_success'),U('Need/index'));
        }
    }

    //删除需求
    public function alldel(){
       $id=I('post.selectedids');
        if($id==''){
             echo $this->echo_json_str("ERROR",L('_no_selected'));exit;
        }else{
           foreach ($id as $v) {
                $obj = M('Needs');
                $where['id'] = $v;
                $where['userID']=$this->userID;
                $obj->where($where)->delete();
           }
           echo $this->echo_json_str("SUCCESS",L('_action_success'),U('Need/index'));exit;
        }
    }
}